package hamsterpolice.todo.model;

import hamsterpolice.todo.data.DataAdapter;
import hamsterpolice.todo.data.DataNotFoundException;
import hamsterpolice.todo.data.Model;
import hamsterpolice.todo.data.UnhandledCriteriaException;
import android.content.Context;
import android.database.Cursor;
import android.text.format.Time;
import android.util.Log;

public class ToDoItem extends Model<ToDoItem> {

	private static final String TAG = "ToDoItem";
	
	private String name;
	private String description;
	private Location location;
	
	/** Using a Long rather than a Time object
	 * so that we can fake an immutable Time object
	 * in the getter and setter.
	 * 
	 * Using a Long rather than a long so that it
	 * can store a null value.
	 */
	private Long dueDate;
	private Category category;
	private User user;
	private boolean completed;

	public ToDoItem(Context context) {
		this(new ToDoItemDataAdapter(context));
	}

	public ToDoItem(DataAdapter<ToDoItem> dataAdapter) {
		super(dataAdapter);
	}
	
	public static ToDoItem get(Context context, long id) throws DataNotFoundException{
		return get(new ToDoItemDataAdapter(context), id);
	}
	
	public static ToDoItem get(DataAdapter<ToDoItem> dataAdapter, long id) throws DataNotFoundException{
		Cursor c = null;
		
		try {
			try {
				c = dataAdapter.get(new IDCriterion(id));
			} catch (UnhandledCriteriaException e) {
				Log.e(TAG, "The Data Adapter's get method is unable to handle an IDCriterion.");
				throw new RuntimeException(e);
			}
			
			if(!c.moveToFirst())
				throw new DataNotFoundException();
			
			ToDoItem item = new ToDoItem(dataAdapter);
			item.setID(dataAdapter.load(c, item));		
			item.setNew(false);
			item.setDirty(false);
			
			return item;
		}
		finally{
			if(c!=null)
				c.close();
		}
	}
	
	public static Cursor getCursor(Context context, ToDoItemFilter filter){
		return getCursor(new ToDoItemDataAdapter(context), filter);
	}
	
	public static Cursor getCursor(DataAdapter<ToDoItem> dataAdapter, ToDoItemFilter filter){
		try {
			return dataAdapter.get(new ToDoItemFilterCriteria(filter));
		} catch (UnhandledCriteriaException e) {
			Log.e(TAG, "The Data Adapter's get method is unable to handle ToDoItemFilterCriteria.");
			throw new RuntimeException(e);
		}
	}

	public void setCompleted(boolean completed) {
		this.completed = completed;
		setDirty(true);
	}

	public boolean isCompleted() {
		return completed;
	}

	public void setName(String name) {
		if (name == null || name.trim().length() == 0)
			throw new IllegalArgumentException("name cannot be null or empty");
		this.name = name;
		setDirty(true);
	}

	public void setDescription(String description) {
		this.description = description;
		setDirty(true);
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
		setDirty(true);
	}

	public void setDueDate(Time dueDate) {
		if (dueDate == null) {
			this.dueDate = null;
		} else {
			this.dueDate = dueDate.toMillis(false);
		}
		setDirty(true);
	}

	public Time getDueDate() {
		if (this.dueDate == null) {
			return null;
		} else {
			Time time = new Time();
			time.set(this.dueDate);
			return time;
		}
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
		setDirty(true);
	}

	public String getName() {
		return name;
	}

	public String getDescription() {
		return description;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
		setDirty(true);
	}



}
